import cn.hutool.core.codec.Base64
import cn.hutool.core.io.resource.ResourceUtil
import cn.hutool.crypto.SecureUtil
import cn.hutool.crypto.asymmetric.KeyType
import cn.hutool.http.HttpUtil
import cn.hutool.json.JSONUtil
import org.junit.Assert
import org.junit.jupiter.api.Test
import org.yaml.snakeyaml.Yaml

class TestUserToken {
    val hm = Yaml().loadAs(ResourceUtil.getStream("application.yml").reader().readText(), TestConfig::class.java)
    val gatewayIp = hm.gatewayip
    var userInfo = JSONUtil.toJsonStr(
        mapOf(
            "username" to hm.user,
            "password" to SecureUtil.rsa(null, hm.publickey).encryptBase64(hm.pass, KeyType.PublicKey),
            "type" to hm.type
        )
    )
    var adminToken = ""

    @Test
    open fun testDoLogin()  {
        val req = HttpUtil.createPost(gatewayIp + "/identification/authentication/token")
        req.timeout(10 * 1000)
        req.body(userInfo)
        req.header("Content-Type", "application/json")
        req.header("Authorization", "Basic ${Base64.encode("truck-client:truck-secret")}")
        val res = req.execute()
        println("res:${res.body()}")
        val resJo = JSONUtil.parseObj(res.body())
        adminToken = resJo.getStr("token_type") + " " + resJo.getStr("access_token")
        println("adminToken:$adminToken")
        val inforeq = HttpUtil.createGet(gatewayIp + "/identification/api/account/info")
        inforeq.header("Authorization", adminToken)
        val infores = inforeq.execute()
        println("infores:${infores.body()}")
        Assert.assertTrue("管理员登录出错", res.status == 200)
    }

}